DAY37:Find the odd int


Posted by birdbirdmurmur on 2023-08-19

題目連結

https://www.codewars.com/kata/54da5a58ea159efa38000836

解法

function findOdd(A) {
    let count = {}
    let min = Infinity

    for (const num of A) {
        count[num] ? count[num]++ : count[num] = 1
    }

    for (const num in count) {
        if (count[num] % 2 !== 0 && min > parseInt(num)) {
            min = parseInt(num)
        }
    }

    return min === Infinity ? 0 : min
}

筆記

同時練習for...offor...in兩種用法

  1. for...of用於迭代陣列等可迭代物件的
  2. for...in用於迭代物件的屬性,包括可數的屬性。

#javascript #Codewars #for...in #for...of #object







Related Posts

發生 Blocking 了!! 怎麼辦?

發生 Blocking 了!! 怎麼辦?

FPGA 基本與設計的一些要點

FPGA 基本與設計的一些要點

漫談傳輸介面-SPI

漫談傳輸介面-SPI


Comments